<?php

global $_GPC;
global $_W;
$shopid = check_right(3);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
load()->model('mc');
$grouplist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' ORDER BY id asc');
$groupary = array();
foreach ($grouplist as $row) {
    $groupary[$row['id']] = $row['companyname'];
}
if ($operation == 'display') {
    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_lotterygame') . ' WHERE weid=\'' . $_W['uniacid'] . '\' order by id desc ');
    $prizelist = array();
    $list2 = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_award') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and isprize=1 order by id asc');
    foreach ($list2 as $row) {
        $prizelist[$row['gid']][] = $row;
    }
} else {
    if ($operation == 'post') {
        $id = intval($_GPC['id']);
        if ($id) {
            $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_lotterygame') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
            $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_award') . ' WHERE gid =\'' . $item['id'] . '\' order by id asc');
        } else {
            $item = array('starttime' => strtotime(date('Y-m-d')), 'endtime' => strtotime(date('Y-m-d')));
            $list = array(0 => '', 1 => '', 2 => '', 3 => '', 4 => '', 5 => '', 6 => '', 7 => '');
        }
    } else {
        if ($operation == 'postdata') {
            $id = intval($_GPC['id']);
            $insert = array('weid' => $_W['uniacid'], 'thumb' => $_GPC['thumb'], 'groupid' => $_GPC['groupid'], 'clientpic' => $_GPC['clientpic'], 'pointer' => $_GPC['pointer'], 'description' => $_GPC['description'], 'title' => $_GPC['title'], 'zppic' => $_GPC['zppic'], 'rule' => htmlspecialchars_decode($_GPC['rule']), 'starttime' => strtotime($_GPC['acttime']['start']), 'endtime' => strtotime($_GPC['acttime']['end']), 'status' => intval($_GPC['status']), 'gzurl' => strval($_GPC['gzurl']), 'sharedes' => strval($_GPC['sharedes']), 'thumb_bg' => strval($_GPC['thumb_bg']), 'thumb_pointer' => strval($_GPC['thumb_pointer']), 'bgcolor' => strval($_GPC['bgcolor']));
            if ($id) {
                pdo_update('j_hmoney_lotterygame', $insert, array('id' => $id));
                foreach ($_GPC['award-level'] as $index => $row) {
                    $data = array('gid' => $id, 'weid' => $_W['uniacid'], 'level' => $_GPC['award-level'][$index], 'leavel' => intval($_GPC['award-leavel'][$index]), 'thumb' => $_GPC['award-thumb'][$index], 'credit' => intval($_GPC['award-credit'][$index]), 'title' => $_GPC['award-title'][$index], 'description' => $_GPC['award-description'][$index], 'isprize' => intval($_GPC['award-isprize'][$index]), 'total' => $_GPC['award-total'][$index], 'renum' => $_GPC['award-renum'][$index], 'probalilty' => $_GPC['award-probalilty'][$index], 'deg' => $_GPC['award-deg'][$index]);
                    pdo_update('j_hmoney_award', $data, array('id' => $index));
                }
            } else {
                $insert['status'] = 1;
                pdo_insert('j_hmoney_lotterygame', $insert);
                $id = pdo_insertid();
                foreach ($_GPC['award-level'] as $index => $row) {
                    $data = array('gid' => $id, 'weid' => $_W['uniacid'], 'level' => $_GPC['award-level'][$index], 'leavel' => intval($_GPC['award-leavel'][$index]), 'thumb' => $_GPC['award-thumb'][$index], 'credit' => intval($_GPC['award-credit'][$index]), 'title' => $_GPC['award-title'][$index], 'description' => $_GPC['award-description'][$index], 'isprize' => intval($_GPC['award-isprize'][$index]), 'total' => $_GPC['award-total'][$index], 'renum' => $_GPC['award-total'][$index], 'probalilty' => $_GPC['award-probalilty'][$index], 'deg' => $_GPC['award-deg'][$index]);
                    pdo_insert('j_hmoney_award', $data);
                }
            }
            exit(json_encode(array('success' => true)));
        } else {
            if ($operation == 'deletetable') {
                $id = intval($_GPC['id']);
                if ($id) {
                    $isUsed = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_lotterygame') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $id));
                    pdo_delete('j_hmoney_lotterygame', array('id' => $id));
                }
                message('删除成功', $this->createWebUrl('lottery'), 'success');
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/lottery');